package com.wdh.kjs.Adapter;

import android.content.Context;
import android.content.Intent;
import android.transition.Visibility;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wdh.kjs.R;
import com.wdh.kjs.activity.InvestmentDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by feifei on 2016/4/20.
 */

public class InvestmentListAdapter extends BaseAdapter {

    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private Context context;
    private HashMap<String, String> maps = new HashMap<String, String>();
    private Intent intent;
    public InvestmentListAdapter(Context context, List<HashMap<String, String>> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_list_of_investment_2, null);
//            viewHolder.tv_investment_type = (TextView) convertView.findViewById(R.id.tv_investment_type);
            viewHolder.tv_investment_name = (TextView) convertView.findViewById(R.id.tv_investment_name);
            viewHolder.tv_investment_available_amount = (TextView) convertView.findViewById(R.id.tv_investment_available_amount);
            viewHolder.item_investment_scale = (TextView) convertView.findViewById(R.id.item_investment_scale);
            viewHolder.item_investment_month = (TextView) convertView.findViewById(R.id.item_investment_month);
            viewHolder.item_investment_financing_amount = (TextView) convertView.findViewById(R.id.item_investment_financing_amount);
            viewHolder.item_investment_state = (LinearLayout) convertView.findViewById(R.id.item_investment_state);
            viewHolder.invest_percent = (TextView) convertView.findViewById(R.id.invest_percent);
            viewHolder.item_investment_progress = (ProgressBar) convertView.findViewById(R.id.item_investment_progress);
            viewHolder.zpf_linear = (LinearLayout) convertView.findViewById(R.id.zpf_linear);
            viewHolder.available_amount_name = (TextView)convertView.findViewById(R.id.available_amount_name);
            viewHolder.item_investment_state_qianggouwancheng = (LinearLayout) convertView.findViewById(R.id.item_investment_state_qianggouwancheng);
            viewHolder.item_investment_state_yurezhong = (LinearLayout) convertView.findViewById(R.id.item_investment_state_yurezhong);
            viewHolder.item_investment_state_huankuanzhong = (LinearLayout) convertView.findViewById(R.id.item_investment_state_huankuanzhong);
            viewHolder.item_investment_state_mujichenggong = (LinearLayout) convertView.findViewById(R.id.item_investment_state_mujichenggong);
//            viewHolder.ceshi = (TextView) convertView.findViewById(R.id.ceshi);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        maps = data.get(position);

        ItemListener itemListener = new ItemListener(position);
        viewHolder.zpf_linear.setOnClickListener(itemListener);
        double a = Double.parseDouble(maps.get("percent"));
        int i= (int) a;
        viewHolder.tv_investment_name.setText(maps.get("tv_investment_name"));
        viewHolder.tv_investment_available_amount.setText(maps.get("tv_investment_available_amount"));
        viewHolder.item_investment_scale.setText(maps.get("item_investment_scale"));
        viewHolder.item_investment_month.setText(maps.get("item_investment_month"));
        viewHolder.item_investment_financing_amount.setText(maps.get("item_investment_financing_amount"));
        viewHolder.invest_percent.setText(maps.get("percent")+"%");
        viewHolder.item_investment_progress.setProgress(i);
//        viewHolder.ceshi.setText(maps.get("item_investment_state"));
        switch (maps.get("item_investment_state")){
            case "抢购":
                viewHolder.item_investment_state.setVisibility(View.VISIBLE);
                viewHolder.item_investment_state_yurezhong.setVisibility(View.GONE);
                viewHolder.item_investment_state_qianggouwancheng.setVisibility(View.GONE);
                viewHolder.item_investment_state_mujichenggong.setVisibility(View.GONE);
                viewHolder.item_investment_state_huankuanzhong.setVisibility(View.GONE);
                break;
            case "预热中":
                viewHolder.item_investment_state_yurezhong.setVisibility(View.VISIBLE);
                viewHolder.item_investment_state.setVisibility(View.GONE);
                viewHolder.item_investment_state_qianggouwancheng.setVisibility(View.GONE);
                viewHolder.item_investment_state_mujichenggong.setVisibility(View.GONE);
                viewHolder.item_investment_state_huankuanzhong.setVisibility(View.GONE);
                break;
            case "募集成功":
                viewHolder.item_investment_state_qianggouwancheng.setVisibility(View.VISIBLE);
                viewHolder.item_investment_state.setVisibility(View.GONE);
                viewHolder.item_investment_state_yurezhong.setVisibility(View.GONE);
                viewHolder.item_investment_state_mujichenggong.setVisibility(View.GONE);
                viewHolder.item_investment_state_huankuanzhong.setVisibility(View.GONE);
                break;
            case "还款中":
                viewHolder.item_investment_state_huankuanzhong.setVisibility(View.VISIBLE);
                viewHolder.item_investment_state_qianggouwancheng.setVisibility(View.GONE);
                viewHolder.item_investment_state.setVisibility(View.GONE);
                viewHolder.item_investment_state_yurezhong.setVisibility(View.GONE);
                viewHolder.item_investment_state_mujichenggong.setVisibility(View.GONE);
                break;
            case "还款完成":
                viewHolder.item_investment_state_mujichenggong.setVisibility(View.VISIBLE);
                viewHolder.item_investment_state_qianggouwancheng.setVisibility(View.GONE);
                viewHolder.item_investment_state.setVisibility(View.GONE);
                viewHolder.item_investment_state_yurezhong.setVisibility(View.GONE);
                viewHolder.item_investment_state_huankuanzhong.setVisibility(View.GONE);
                break;
            default:break;
        }
        return convertView;
    }

    class ViewHolder {
//        TextView tv_investment_type;
        TextView tv_investment_name;
        TextView tv_investment_available_amount;
        TextView item_investment_scale;
        TextView item_investment_month;
        TextView item_investment_financing_amount;
        LinearLayout item_investment_state;
        TextView invest_percent;
        ProgressBar item_investment_progress;
        LinearLayout zpf_linear;
        TextView available_amount_name;
        LinearLayout item_investment_state_yurezhong;
        LinearLayout item_investment_state_qianggouwancheng;
        TextView ceshi;
        LinearLayout item_investment_state_huankuanzhong;
        LinearLayout item_investment_state_mujichenggong;
    }

    private class ItemListener implements View.OnClickListener {

        private int m_position;
        public ItemListener(int position) {
            this.m_position = position;
        }

        @Override
        public void onClick(View v) {
            int loadId = Integer.parseInt(data.get(m_position).get("loadId"));
            String status = data.get(m_position).get("status");
            String state=data.get(m_position).get("item_investment_state");
            intent = new Intent(context ,InvestmentDetailActivity.class);
            intent.putExtra("itemId", loadId+"");
            intent.putExtra("status", status+"");
            intent.putExtra("item_investment_state", state);
            context.startActivity(intent);
        }
    }
}